package com.thf.controller;

import com.alibaba.fastjson.JSON;
import com.thf.model.ResultData;
import com.thf.util.SendSMS;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginPhoneCodeVerifyController {
    private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    @RequestMapping("/login/phoneCode")
    protected void loginPhoneCodeVerify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //     0。乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String phone = req.getParameter("phone");
//        System.out.println(phone);

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(Url);
        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        int mobile_code = (int)((Math.random()*9+1)*100000);
        String content = "您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。";
//        //将生成的验证码存放到session中
//        req.getSession().setAttribute("smsCode", mobile_code);
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "C89896956"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
//                new NameValuePair("password", "8b13ed0b43e937c5d007291b00229747"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                new NameValuePair("password", SendSMS.MD5Encode("8b13ed0b43e937c5d007291b00229747")),
                new NameValuePair("mobile", phone),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);

            String SubmitResult =method.getResponseBodyAsString();

            //System.out.println(SubmitResult);

            Document doc = DocumentHelper.parseText(SubmitResult);
            Element root = doc.getRootElement();

            String code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

//            System.out.println(code);
//            System.out.println(msg);
//            System.out.println(smsid);

//            if("2".equals(code)){
//                System.out.println("短信提交成功");
//            }
            //将生成的验证码放到redis中，键60秒过期
//          jedis.set("smsCode", String.valueOf(mobile_code));
//          jedis.expire("smsCode",60);
            if("2".equals(code)){
                Jedis jedis = new Jedis();
                jedis.setex("smsCode",60*60*60,String.valueOf(mobile_code));
            }
            ResultData result =  new ResultData(Integer.valueOf(code),msg);
            String resJson = JSON.toJSONString(result);
            resp.getWriter().println(resJson);

        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
